package com.forum.demo.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.forum.demo.common.ResultCode;
import com.forum.demo.mapper.BoardMapper;
import com.forum.demo.model.Board;
import com.forum.demo.services.IBoardService;
import com.forum.demo.utils.checkFailUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        // 1. 非空校验
        if (num < 0) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        List<Board> boards = boardMapper.selectList(new LambdaQueryWrapper<Board>()
                .eq(Board::getState, 0)
                .eq(Board::getDeleteState, 0)
                .orderByAsc(Board::getId)
                .last("limit 0," + num));
        return boards;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        // 1. 非空校验
        if (id == null || id <= 0) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Board board = boardMapper.selectOne(new LambdaQueryWrapper<Board>()
                .eq(Board::getId, id)
                .eq(Board::getState, 0)
                .eq(Board::getDeleteState, 0));
        if (board == null) {
            checkFailUtils.checkFail(ResultCode.ERROR_IS_NULL);
        }
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        boardMapper.update(updateBoard,new LambdaQueryWrapper<Board>()
                .eq(Board::getId, id));
    }

    @Override
    public void subOneArticleCountById(Long id) {
        // 1. 非空校验
        if (id == null || id <= 0) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Board board = boardMapper.selectOne(new LambdaQueryWrapper<Board>()
                .eq(Board::getId, id)
                .eq(Board::getState, 0)
                .eq(Board::getDeleteState, 0));
        if (board == null) {
            checkFailUtils.checkFail(ResultCode.ERROR_IS_NULL);
        }
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        boardMapper.update(updateBoard,new LambdaQueryWrapper<Board>()
                .eq(Board::getId, id));
    }

    @Override
    public Board selectById(Long id) {
        if (id == null) {
            checkFailUtils.checkFail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Board board = boardMapper.selectOne(new LambdaQueryWrapper<Board>()
                .eq(Board::getId, id)
                .eq(Board::getState, 0)
                .eq(Board::getDeleteState, 0));
        if (board == null) {
            checkFailUtils.checkFail(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        return board;
    }
}
